package com.javaweb.spring.controller;

import com.javaweb.spring.service.UserService;
import com.javaweb.spring.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService userService=new UserServiceImpl();
        String username=request.getParameter("name");
        String password=request.getParameter("password");
        int count=userService.login(username,password);
        if (count==1){
            response.sendRedirect(request.getContextPath()+"/loginSucess.jsp");
        }else if(count==0){
            response.sendRedirect(request.getContextPath()+"/passwordError.jsp");
        }else {
            response.sendRedirect(request.getContextPath()+"/usernameError.jsp");
        }
    }
}
